<template>
  <div ref="chartRef" style="width: 100%; height: 300px;"></div>
</template>

<script setup>
import { ref, onMounted, onUnmounted, watch } from 'vue'
import * as echarts from 'echarts'

const props = defineProps({
  data: {
    type: Array,
    default: () => []
  },
  showRevenue: {
    type: Boolean,
    default: false
  },
  showBoth: {
    type: Boolean,
    default: false
  }
})

const chartRef = ref(null)
let chartInstance = null

const initChart = () => {
  if (!chartRef.value) return
  
  if (!chartInstance) {
    chartInstance = echarts.init(chartRef.value)
  }
  
  const isBoth = props.showBoth
  const isRevenueOnly = props.showRevenue && !props.showBoth
  
  const option = {
    title: {
      text: isRevenueOnly ? '订单收入趋势' : isBoth ? '订单趋势与收入' : '订单趋势',
      left: 'center',
      textStyle: {
        fontSize: 16
      }
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross'
      }
    },
    legend: {
      data: isBoth ? ['订单数', '收入'] : isRevenueOnly ? ['收入'] : ['订单数'],
      top: 30
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: props.data.map(item => item.date)
    },
    yAxis: (isBoth || isRevenueOnly) ? [
      {
        type: 'value',
        name: isRevenueOnly ? '收入(¥)' : '订单数',
        position: 'left'
      },
      ...(isBoth ? [{
        type: 'value',
        name: '收入(¥)',
        position: 'right'
      }] : [])
    ] : [{
      type: 'value',
      name: '订单数'
    }],
    series: isBoth ? [
      {
        name: '订单数',
        type: 'line',
        data: props.data.map(item => item.count),
        smooth: true,
        itemStyle: { color: '#409EFF' }
      },
      {
        name: '收入',
        type: 'line',
        yAxisIndex: 1,
        data: props.data.map(item => item.revenue),
        smooth: true,
        itemStyle: { color: '#67C23A' }
      }
    ] : isRevenueOnly ? [{
      name: '收入',
      type: 'line',
      data: props.data.map(item => item.revenue),
      smooth: true,
      areaStyle: {
        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
          { offset: 0, color: 'rgba(103, 194, 58, 0.3)' },
          { offset: 1, color: 'rgba(103, 194, 58, 0.1)' }
        ])
      },
      itemStyle: { color: '#67C23A' }
    }] : [{
      name: '订单数',
      type: 'line',
      data: props.data.map(item => item.count),
      smooth: true,
      areaStyle: {
        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
          { offset: 0, color: 'rgba(64, 158, 255, 0.3)' },
          { offset: 1, color: 'rgba(64, 158, 255, 0.1)' }
        ])
      },
      itemStyle: { color: '#409EFF' }
    }]
  }
  
  chartInstance.setOption(option)
}

const resizeChart = () => {
  if (chartInstance) {
    chartInstance.resize()
  }
}

watch(() => [props.data, props.showRevenue, props.showBoth], () => {
  initChart()
}, { deep: true })

onMounted(() => {
  initChart()
  window.addEventListener('resize', resizeChart)
})

onUnmounted(() => {
  window.removeEventListener('resize', resizeChart)
  if (chartInstance) {
    chartInstance.dispose()
    chartInstance = null
  }
})
</script>

